<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:insert="common/person-pc/header::personHeadBar('绑定邮箱',~{})">
<body>
<div class="el-row">
    <form name='theForm' id="theForm" method="post">
        <div class="ey-popup">
            <div class="el-form-item">
                <div class="el-input">
                    <input type="text" name="email" id="email" required class="el-input__inner" autocomplete="off"
                           placeholder="邮箱地址">
                </div>
            </div>
            <div class="el-form-item">
                <div class="el-input el-input-group el-input-group--append el-input-group--prepend">
                    <input type="text" class="el-input__inner" id="email_code" name="emailCode" autocomplete="off"
                           placeholder="邮箱验证码">
                    <div class="el-input-group__append">
                        <input type="button" id="email_button" onclick="get_email_code();"
                               class="el-button el-button--default" value="点击发送"/>
                    </div>
                </div>
            </div>
            <div class="el-form-item">
                <button type="button" class="el-button el-button--primary" style="width: 100%;" onclick="SubmitData();">
                    确定
                </button>
            </div>

        </div>
    </form>
</div>

<script type="text/javascript">
    function get_email_code() {
        var email = $("#email").val();
        var reg = /^[a-z0-9]([a-z0-9\\.]*[-_]{0,4}?[a-z0-9-_\\.]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+([\.][\w_-]+){1,5}$/i;
        // 邮箱格式不正确
        if (!reg.test(email)) {
            layer.msg('请正确输入邮箱地址！', {time: 2000});
            return false;
        }
        $("#email_button").val('发送中…');
        $("#email_button").attr('disabled', 'disabled');
        $.ajax({
            url: "/res-login/sendEmail.html",
            data: {email: email, title: '修改邮箱', type: 3},
            type: 'get',
            success: function (res) {
                layer.closeAll();
                if (res.rt == -1) {
                    $("#email_button").val('点击发送');
                    $("#email_button").removeAttr("disabled");
                    showErrorAlert('参数有误');
                    return;
                }
                if (res.rt == -2) {
                    $("#email_button").val('点击发送');
                    $("#email_button").removeAttr("disabled");
                    showErrorAlert('邮箱已被绑定');
                    return;
                }
                if (res.rt == -3) {
                    $("#email_button").val('点击发送');
                    $("#email_button").removeAttr("disabled");
                    showErrorAlert('邮箱不存在');
                    return;
                }
                if (res.rt == 500) {
                    showErrorMsg("网络出现异常,请稍后重试!");
                    return;
                }
                if (res.rt == 1) {
                    layer.msg("发送成功", {icon: 6});
                    countdown();
                }
            },
            error: function (e) {
                layer.closeAll();
                $("#email_button").val('点击发送');
                $("#email_button").removeAttr("disabled");
                showErrorAlert("网络异常,请稍后再试!");
            }
        });


    }

    function SubmitData() {
        var email = $("#email").val();
        var reg = /^[a-z0-9]([a-z0-9\\.]*[-_]{0,4}?[a-z0-9-_\\.]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+([\.][\w_-]+){1,5}$/i;
        // 邮箱格式不正确
        if (!reg.test(email)) {
            layer.msg('请正确输入邮箱地址！', {time: 2000});
            return false;
        }
        var email_code = $("#email_code").val();
        if (!email_code) {
            layer.msg('邮箱验证码不能为空！', {time: 2000});
            return false;
        }
        var _parent = parent;
        var parentObj = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        layer_loading('正在处理');
        $.ajax({
            url: "/user/bindEmail.html",
            data: {
                "userEmail": email,
                "emailCode": email_code
            },
            type: 'post',
            success: function (res) {
                layer.closeAll();
                if (res.rt == 1) {
                    parent.layer.close(parentObj);
                    parent.layer.msg("绑定成功", {time: 1500}, function () {
                        _parent.window.location.reload();
                    });
                }
                if (res.rt == -1) {
                    showErrorAlert("未登录,请登录！");
                } else if (res.rt == -2) {
                    showErrorMsg("邮箱已被注册过！");
                    return;
                } else {
                    showErrorAlert(res.msg);
                }
            },
            error: function (e) {
                layer.closeAll();
                showErrorAlert("网络异常,请稍后再试！");
            }
        });
    };


    function countdown() {
        // 倒计时
        var setTime;
        var time = 120;
        setTime = setInterval(function () {
            if (0 >= time) {
                clearInterval(setTime);
                return;
            }
            time--;
            $("#email_button").val(time + '秒');
            $("#email_button").attr('disabled', 'disabled');

            if (time == 0) {
                $("#email_button").val('点击发送');
                $("#email_button").removeAttr("disabled");
            }
        }, 1000);
    };
</script>
</body>
</html>
